package com.example.controller;

import com.example.entity.StudentCourse;
import com.example.service.StudentCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentGradesController {

    @Autowired
    private StudentCourseService studentCourseService;

    @GetMapping("/grades")
    public String viewGrades(
            @RequestParam(required = false) Integer id,
            HttpSession session,
            Model model) {

        // 检查登录状态
        if (session.getAttribute("loggedIn") == null) {
            return "redirect:/index";
        }

        Integer studentId = id != null ? id : (Integer) session.getAttribute("studentId");
        List<StudentCourse> grades = studentCourseService.getGradesByStudentId(studentId);

        model.addAttribute("grades", grades);
        model.addAttribute("userType", session.getAttribute("userType"));
        return "student-grades";
    }
}